#include"DetectorManager.h"
using namespace Log;

std::unordered_map<int, DetectorInfoPtr> DetectorManager::DetectorInfoMap;

DetectorManager::DetectorManager() {
	for (auto& it : DetectorInfoMap) {
		detectorPools[it.second->groupID] = std::make_shared<DetectorPool>(it.first,it.second);
	}
	LOG_INFO << "Init DetectorPools Number: " << static_cast<int>(DetectorInfoMap.size());
}

DetectorManager::~DetectorManager() = default;

void DetectorManager::setDetectorInfos(const std::vector<DetectorInfoPtr>& detectorInfos) {
	for (auto& it : detectorInfos) {
		if (DetectorInfoMap.find(it->groupID) != DetectorInfoMap.end()) {
			LOG_WARN << "DetectorConfigPtr.groupID: " << it->groupID << " already exits!";
		}
		else {
			DetectorInfoMap[it->groupID] = it;
		}
	}
}

std::vector<BBox> DetectorManager::detect(int groupID, cv::Mat image, double threshold) {
	assert(detectorPools.find(groupID) != detectorPools.end());
	return detectorPools[groupID]->detect(image, threshold);
}

DetectorResPtr DetectorManager::getDetector(int groupID, int id) {
	assert(detectorPools.find(groupID) != detectorPools.end());
	return detectorPools[groupID]->getDetector(id);
}

void DetectorManager::putDetector(int groupID, DetectorResPtr detectorPtr) {
	assert(detectorPools.find(groupID) != detectorPools.end());
	return detectorPools[groupID]->putDetector(detectorPtr);
}